package com.cpp.util;

import java.util.Date;
import java.util.List;

import com.cpp.bean.Tbl_Sina_token;
import com.cpp.db.DBUtils;
import com.cpp.db.ResultRow;

public class TokenUtil {

	public static Tbl_Sina_token getToken(){
		Tbl_Sina_token tbl_sina_token = new Tbl_Sina_token();
		String sql = "select id,token,createtime from tbl_sina_token where flag = '0' order by createtime desc";
		try {
			List<ResultRow> list = DBUtils.query(sql);
			if(list.size() > 0){
				ResultRow rr = list.get(0);
				tbl_sina_token.setId(rr.getInt(1));
				tbl_sina_token.setToken(rr.getString(2));
				tbl_sina_token.setCreatetime(rr.getDate(3));
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return tbl_sina_token;
	}
	
	public static void updateToken(String token){
		String query = "select count(*) from tbl_sina_token where token = ?";
		try {
			int num = DBUtils.queryInt(query, new Object[]{token});
			if(num <= 0){
				String sql = "insert into tbl_sina_token(token,createtime,flag) values(?,?,?)";
				Object[] params = new Object[]{token,new Date(),"0"};
				DBUtils.update(sql, params);
			}
		} catch (Exception e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
	}
}
